﻿using System.Linq;
using www.Models;

namespace Data.DAO
{
    public class StoryValueDAO//:BaseDAO<RELAXEntities,RelaxStory>
    {
        #region value
        private RelaxDataContext _context = new RelaxDataContext();
        StoryRefDAO _storyRefDao=new StoryRefDAO();
        #endregion

        #region Ham lay du Lieu

        #endregion 
    
        #region Add
        public void Add(NEWS_VALUE story)
        {
            _context.NEWS_VALUEs.InsertOnSubmit(story);
            _context.SubmitChanges();

        }
        #endregion

        #region Ham Update

        public bool Update(NEWS_VALUE story)
        {
            var firstOrdefault = _context.NEWS_VALUEs.FirstOrDefault(c => c.newid == story.newid && c.languagecode==story.languagecode);
            if (firstOrdefault!=null)
            {
                firstOrdefault.name = story.name;
                firstOrdefault.tag = story.tag;
                firstOrdefault.keyword = story.keyword;
                firstOrdefault.decription = story.decription;
                firstOrdefault.detail = story.detail;
                
                _context.SubmitChanges();
                return true;
            }
            return false;
        }
        #endregion 

        #region Ham Delete

        public void Delete(long iId, long iLanguageCode)
        {
            var query = _context.NEWS_VALUEs.Where(c => c.newid == iId);
            var deleteRef = query.Count() == 1 || iLanguageCode == 0;
            foreach (var item in query)
            {
                _context.NEWS_VALUEs.DeleteOnSubmit(item);
            }
            _context.SubmitChanges();
           if(deleteRef)
               _storyRefDao.Delete(iId);
        }
        #endregion
    }
}
